﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinFormAssigment.Entities;
using WinFormAssigment.DAO;
using System.Data;
using System.Data.SqlClient;

namespace WinFormAssigment.BUS
{
    class Login
    {
        public static Teacher TeacherLogin(string user, string pass)
        {
            SqlParameter[] sqlParameter = Query.TeacherLogin(user, pass);
            string stored = Query.teacherLogin;
            DataTable table = DatabaseInteraction.GetSelectTable(stored, sqlParameter);
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    string teacherID = row[0].ToString();
                    string password = row[1].ToString();
                    string name = row[2].ToString();
                    string gender = row[3].ToString();
                    DateTime dob = Convert.ToDateTime(row[4].ToString());
                    string address = row[5].ToString();
                    string email = row[6].ToString();
                    string phone = row[7].ToString();
                    string description = row[8].ToString();                    
                    if (user.Contains(teacherID) && pass.Contains(password))
                    {
                        Teacher teacher = new Teacher()
                        {
                            TeacherID = teacherID,
                            Password = password,
                            Name = name,
                            Gender = gender,
                            Dob = dob,
                            Address = address,
                            Email = email,
                            Phone = phone,
                            Description = description
                        };
                        return teacher;
                    }
                }
            }
            return null;
        }

        public static Manager ManagerLogin(string user, string pwd)
        {
            SqlParameter[] sqlParameters = Query.ManagerLogin(user, pwd);
            string stored = Query.managerLogin;
            DataTable table = DatabaseInteraction.GetSelectTable(stored, sqlParameters);
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    string managerID = row[0].ToString();
                    string password = row[1].ToString();
                    string name = row[2].ToString();
                    string gender = row[3].ToString();
                    DateTime dob = Convert.ToDateTime(row[4].ToString());
                    string address = row[5].ToString();
                    string email = row[6].ToString();
                    string phone = row[7].ToString();
                    string description = row[8].ToString();
                    if (user.Contains(managerID) && pwd.Contains(password))
                    {
                        Manager manager = new Manager()
                        {
                            ManagerID = managerID,
                            Password = password,
                            Name = name,
                            Gender = gender,
                            Dob = dob,
                            Address = address,
                            Email = email,
                            Phone = phone,
                            Description = description
                        };
                        return manager;
                    }
                }
            }
            return null;
        }

    }
}
